<?php

//计算范围，可以做搜索用户
function GetRange($lat, $lon, $raidus) {
    define('PI', 3.1415926535898);
    //计算纬度
    $degree = (24901 * 1609) / 360.0;
    $dpmLat = 1 / $degree;
    $radiusLat = $dpmLat * $raidus;
    $minLat = $lat - $radiusLat; //得到最小纬度
    $maxLat = $lat + $radiusLat; //得到最大纬度     
    //计算经度
    $mpdLng = $degree * cos($lat * (PI / 180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng * $raidus;
    $minLng = $lon - $radiusLng;  //得到最小经度
    $maxLng = $lon + $radiusLng;  //得到最大经度
    //范围
    $range = array(
        'minLat' => $minLat,
        'maxLat' => $maxLat,
        'minLon' => $minLng,
        'maxLon' => $maxLng
    );
    return $range;
}

function getAreaName($type = 0) {
    $area = getSubField("area", cookie('area'));
    if ($type == 1) {
        return "<a href=" . U("Area/lists", array("id" => $area['id'])) . ">" . $area['name'] . "</a>";
    } else {
        return $area['name'];
    }
}

function getreplylist($id) {
    $huifu = M("say")->where("pid=" . $id . "")->order('id ASC')->limit(10)->select();
    foreach ($huifu as $v) {
        $user = M("user")->field("avatar,name")->where("id = " . $v['uid'] . "")->find();
        echo "<dd style='padding-left:45px'id='dd_" . $v['id'] . "'><div class='userPic30'>
<img src='" . getUserAvatar($user['avatar'], 130) . "'>
</div><div class='userTalkFont2'><p>
<span class='org'>" . $user['name'] . "</span>：" . ubbReplace($v['content']) . "</p><h4>" . tranTime($v['addtime']) . "
    <a class='org' onclick='reply(" . $v['pid'] . "," . $v['id'] . ")'>回复</a>
</h4></div></dd>";
    }
}

function ubbReplace($str) {
    $str = str_replace("<", '<；', $str);
    $str = str_replace(">", '>；', $str);
    $str = str_replace("\n", '>；br/>；', $str);
    $str = preg_replace("[\[em_([0-9]*)\]]", "<img src=\"" . __APP__ . "/Public/face/$1.gif\" />", $str);
    return $str;
}

function delArea($id) {
    $lists = M("area")->field("id")->where("pid = " . $id . "")->select();
    if ($lists) {
        foreach ($lists as $v) {
            delArea($v['id']);
        }
        M("area")->where("pid = " . $id . "")->delete();
    }
}

function getdish_type($id) {
    $info = getSubField("dish_types", "name,id", "1=1", "ord ASC", $id);

    return $info['name'];
}

function getShopState($business_time, $weeks, $is_busy) {
    if ($is_busy == 1) {
        $word = "太忙暂不送餐";
    } else if ($is_busy == 2) {
        $word = "休息已放假";
    } else {
        $weeks = explode(",", $weeks);
        $week = date("w");
        if ($week == 0) {
            $week = 7;
        }
        if (!in_array($week, $weeks)) {
            $word = "休息已放假";
        } else {
            $business_time = explode(",", $business_time);
            $hour = date("H");
            $minutes = date("i");
            $now = $hour * 60 + $minutes;
            $start = $business_time[0] * 60 + $business_time[1];
            $end = $business_time[2] * 60 + $business_time[3];
            $start2 = $business_time[4] * 60 + $business_time[5];
            $end2 = $business_time[6] * 60 + $business_time[7];
            if ($start + $end + $start2 + $end2 == 0) {
                $word = "";
            } else {
                if (($now > $start && $now < $end) or ( $now > $start2 && $now < $end2)) {
                    $word = "";
                } elseif ($now > $end && $now < $start2) {

                    $word = "" . $business_time[4] . ":" . getTwoTime($business_time[5]) . "开始订餐";
                } else {
//                    $word = "" . $business_time[0] . ":" . getTwoTime($business_time[1]) . "开始订餐";
                    $word = "已打烊";
                }
            }
        }
    }
    return $word;
}

function getTwoTime($t) {
    if (strlen($t) == 1) {
        $t = "0" . $t;
    }
    return $t;
}

function getShopCla($business_time, $weeks, $is_busy) {
    $cla = 'si_closed';
    if ($is_busy == 0) {
        $weeks = explode(",", $weeks);
        $week = date("w");
        if ($week == 0) {
            $week = 7;
        }
        if (in_array($week, $weeks)) {
            $business_time = explode(",", $business_time);
            $hour = date("H");
            $minutes = date("i");
            $now = $hour * 60 + $minutes;
            $start = $business_time[0] * 60 + $business_time[1];
            $end = $business_time[2] * 60 + $business_time[3];
            $start2 = $business_time[4] * 60 + $business_time[5];
            $end2 = $business_time[6] * 60 + $business_time[7];
            if ($start + $end + $start2 + $end2 == 0) {
                $cla = "";
            } else {
                if (($now > $start && $now < $end) or ( $now > $start2 && $now < $end2)) {
                    $cla = "";
                }
            }
        }
    }
    return $cla;
}

function getGoodsAttr($attr) {
    $pics = '';
    if ($attr) {
        $attrArr = explode(",", $attr);
        foreach ($attrArr as $v) {
            $name = getStaticInfo($v, 'dictionary', 'name');
            $logo = getStaticInfo($v, 'dictionary', 'logo');
            $pics .= "<img style='margin-right:2px;position:relative;top:3px;' src='" . C("SITE_URL") . "/" . C("shopsFile") . "/" . $logo . "' alt=" . $name . " title=" . $name . " />";
        }
    }
    return $pics;
}

function getCheckboxCla($ids, $id, $cla) {
    if (!empty($ids)) {
        //  $ids = explode(",", $ids);
        if (in_array($id, $ids)) {
            return $cla;
        }
    }
}

function getcartTotal($a, $b) {
    return getOneDot($a * $b);
}

function get_rand($proArr) {
    $result = ''; //概率数组的总概率精度 
    $proSum = array_sum($proArr);
    foreach ($proArr as $key => $proCur) {//概率数组循环
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $proCur) {
            $result = $key;
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset($proArr);
    return $result;
}

function is_shop_admin($uid) {
    if ($uid == -1) {
        return "管理员";
    } else {
        $info = M("shop")->field("id")->where("uid = " . $uid . "")->find();
        if ($info) {
            return "商家";
        } else {
            $info = M("user")->field("name")->where("id = " . $uid . "")->find();
            return $info['name'];
        }
    }
}

function getCommentTotal($total, $star) {
    if ($star == 1) {
        return round($total / 3 * 2, 1);
    } else {
        return getStar($total / 3 * 2);
    }
}

function getIntval($num) {
    return intval($num);
}

function getShopAttr($attr) {
    $lists = array(
        0=>array("id"=>1,"name"=>"减价","logo"=>"Public/images/jian.png"),
        1=>array("id"=>2,"name"=>"赔","logo"=>"Public/images/pei.png"),
        2=>array("id"=>3,"name"=>"发票","logo"=>"Public/images/fapiao.png"),
        3=>array("id"=>4,"name"=>"券","logo"=>"Public/images/quan.png"),
        4=>array("id"=>5,"name"=>"赠","logo"=>"Public/images/zeng.png")
    );
    if ($attr) {
        $attrs = explode(",", $attr);
        $imgs = '';
        foreach ($lists as $v) {
            if (in_array($v['id'], $attrs)) {
                $imgs.= "<img src='" . __APP__ . "/" . $v['logo'] . "' style='margin-right:3px;' title=" . $v['name'] . ">";
            }
        }
        return $imgs;
    }
}

function getSign($row) {
    $t = $row + 1;
    if ($t > date('d')) {
        $td = "<td   width = '90px' style='background-color:lemonchiffon' valign='top'>
<div align='right' valign='top'><span style='position:relative;right:20px;'>" . $t . "</span>
</div><div align='left'> </div><div align='left'> </div></td>";
    } else {
        if (strlen($t) == 1) {
            $day = "0" . $t;
        } else {
            $day = $t;
        }
        $t2 = strtotime(date("Y-m-" . $day . ""));
        $info = M("sign")->field("id")->where("addtime = " . $t2 . " AND status = 0 AND uid = " . session('userid') . "")->find();
        //echo $Dao->getLastSql();
        if ($info) {
            $td = "<td  width = '90px' style='background-color:navajowhite;navajowhite ;'>
<div align='right' valign='top'><span style='position:relative;right:20px;'>" . $t . "</span>
</div><div align='left'>
<img width='35px' height='35px' src='" . __APP__ . "/Public/images/cart_3.gif' style='position:relative;left:10px;'> 已签到
</div></td>";
        } else {
            if ($t == date('d')) {
                $td = "<td  width='90px' style='background-color:#00bb00;color:blue' onclick='signDay($(this))'>
<div align='right' valign='top'><span style='position:relative;right:20px;'>" . $t . "</span></div>
<div align='center'><a style='cursor:pointer;color:#ffffff;' >签到</a></div></td>";
            } else {
                $td = "<td  width = '90px' style='background-color:#DCDCDC;'>
<div align='right' valign='top'><span style='position:relative;right:20px;'>" . $t . "</span>
</div><div align='left'style='height:47px'>
</div></td>";
            }
        }
    }
    return $td;
}

function getTimes() {
    $now = date("H") * 60 + date("i");
    $other = 5 - $now % 5;
    $start = $now + $other;
    for ($i = 0; $i < 12; $i++) {
        $times[$i]['t'] = intval(($start + $i * 15) / 60) . ":" . getTimeType(($start + $i * 15) % 60);
        $times[$i]['t2'] = $start + $i * 15;
    }
    return $times;
}



?>
